package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.User;
import com.cxs.xiaomi.exception.ServletResultNotFountException;
import com.cxs.xiaomi.utils.BeanAsJsonUtil;
import cxs.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

/*
 * @Author:cxs
 * @Date:2021/9/14  15:51
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("method");
        if (methodName != null && methodName.trim().length() > 0) {
            try {
                Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
                Object invokeReturn = method.invoke(this, request, response);
                if (invokeReturn != null) {
                    if (String.class.isInstance(invokeReturn)) {
                        String invokeReturnString = (String) invokeReturn;
                        String[] urls = invokeReturnString.split(":");
                        if ("redirect".equals(urls[0])) {
                            response.sendRedirect(request.getContextPath() + urls[1]);
                        } else if ("forward".equals(urls[0])) {
                            request.getRequestDispatcher(urls[1]).forward(request, response);
                        } else {
                            throw new ServletResultNotFountException(urls[0] + " is underfined!");
                        }
                    } else if (Result.class.isInstance(invokeReturn)) {
                        Result result = (Result) invokeReturn;
                        String jsonString = BeanAsJsonUtil.tran(result, response);
                        response.getWriter().write(jsonString);
                    } else {
                        throw new RuntimeException("Class Type is not Such(类型不匹配)");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // 获取已登录的用户
    protected User getUser(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session != null) {
            Object userObj = session.getAttribute("user");
            if (userObj != null) {
                return (User) userObj;
            }

        }
        return null;
    }

    // 获得登录管理员
    protected User getAdmin(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        if (session != null) {
            Object userObj = session.getAttribute("admin");
            if (userObj != null) {
                return (User) userObj;
            }
        }
        return null;
    }
}
